import BaseConfig, { IItemConfig } from "../../Script/Base/BaseConfig";
import BaseView from "../../Script/Base/BaseView";
import { EBundleType, EResType, EViewType } from "../../Script/Common/Enum";
import { GlobalData } from "../../Script/Common/GlobalData";
import { IViewData } from "../../Script/Interface/IMessage";
import { SceneGame } from "../../Script/SceneGame";
import AppService from "../../Script/Service/AppService";
import { LoadUtil } from "../../Script/Util/LoadUtil";
import GameUI from "./GameUI";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'ItemUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};
@ccclass
export default class ItemUI extends BaseView {

    @property({ type: cc.Sprite, tooltip: "物品icon" })
    private icon: cc.Sprite = null;

    private config: IItemConfig = null;

    protected onLoad() {
        super.onLoad();

        this.refreshUI();
    }

    private refreshUI() {
        this.config = BaseConfig.instance.getItemInfoById(this.prepareData.itemId);
        if (this.config) {
            LoadUtil.instance.setSpriteFrame(this.icon, EResType.Item, this.config.id, 100, 100);
        }
        // 添加到背包中
        GlobalData.addBagData(this.config.id, this.prepareData.num);
    }

    private clickItem() {
        this.close();
        SceneGame.instance.showToast(cc.v2(0, 0), '物品已放入背包');
    }

    private close() {
        SceneGame.instance.removePopView(this.node.name);
        SceneGame.instance.hideGuideUI();
        GameUI.instance.check();
    }
}
AppService.instance.registViewData(VIEW_DATA);